package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.entity.SummerWinterDate;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.educational.service.IRoomService;
import com.yuyou.fn.educational.service.ISummerWinterDateService;
import com.yuyou.fn.educational.vo.CampusQueryCondVo;
import com.yuyou.fn.educational.vo.CampusSaveOrUpdateVo;
import com.yuyou.fn.educational.vo.RoomQueryVo;
import com.yuyou.fn.educational.vo.RoomSaveOrUpdateVo;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.settlement.entity.MainAccount;
import com.yuyou.fn.tradingservice.entity.ServiceCategory;
import com.yuyou.fn.tradingservice.entity.ServiceMerchant;
import com.yuyou.fn.tradingservice.entity.ServiceOrderItem;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.service.IServiceCategoryService;
import com.yuyou.fn.tradingservice.service.IServiceMerchantService;
import com.yuyou.fn.tradingservice.service.IServiceOrderItemService;
import com.yuyou.fn.tradingservice.service.IServiceProductService;
import com.yuyou.fn.tradingservice.vo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by itachi on 2017/04/05.
 */
@Controller
@RequestMapping("/api/admin/trading")
public class AdminTradingController extends ApiBaseController {

    @Resource
    private IServiceCategoryService categoryService;

    @Resource
    private IServiceProductService productService;

    @Resource
    private IServiceMerchantService serviceMerchantService;


    @Resource
    private IRoomService roomService ;

    @Resource
    private ICampusService campusService;

    @Resource
    private IServiceOrderItemService serviceOrderItemService;

    @Resource
    private IServiceProductService serviceProductService;

    @Resource
    private ISummerWinterDateService summerWinterDateService;


    @ApiOperation("服务商家列表")
    @RequestMapping(value = "/merchantList")
    @ResponseBody
    public ReturnResult<Page<ServiceMerchant>> merchantList(@ModelAttribute ServiceMerchantVo serviceMerchantVo,@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpServletRequest request){
        Page<ServiceMerchant> page = serviceMerchantService.findMerchantPage(serviceMerchantVo,pageNo,pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("审核")
    @RequestMapping(value = "/auditMerchant", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<String> auditMerchant(ServiceMerchantVo serviceMerchantVo , HttpServletRequest request,
                                              HttpSession session) {
        serviceMerchantService.auditMerchants(serviceMerchantVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("新增多个树状分类")
    @RequestMapping(value = "/saveCategoryList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveCategoryList(@ModelAttribute CategoryCommonVo categoryCommonVo, @RequestParam(value = "categoryTreeVoJson") String categoryTreeVoJson) {
        //categoryService.saveCategory(categoryVo);
        CategoryTreeVo categoryTreeVo = null;
        if (StringUtils.isNotEmpty(categoryTreeVoJson)) {
            categoryTreeVo = JSONObject.parseObject(categoryTreeVoJson, CategoryTreeVo.class);
        } else {
            return ReturnResult.fail("json为空或者格式不正确");
        }
        categoryService.saveCategoryList(categoryCommonVo,categoryTreeVo);
        return ReturnResult.success("Ok");
    }


    @ApiOperation("得到某种type的分类")
    @RequestMapping(value = "/findServiceCategoryTreeVoByType", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> findServiceCategoryTreeVoByType(@RequestParam(value = "type",required = false) Integer type) {
        if(type == null ){
            return ReturnResult.success(Collections.EMPTY_LIST);
        }
        List<ServiceCategoryTreeVo> serviceCategoryTreeVoList=categoryService.findServiceCategoryTreeVoByType(type);
        return ReturnResult.success(serviceCategoryTreeVoList);
    }


    @ApiOperation("保存分类信息")
    @RequestMapping(value = "/saveCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveCategory(ServiceCategoryVo categoryVo) {
        categoryService.saveCategory(categoryVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("更新分类信息")
    @RequestMapping(value = "/editCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> editCategory(ServiceCategoryVo categoryVo) {
        categoryService.editCategory(categoryVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("删除分类信息")
    @RequestMapping(value = "/deleteCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteCategory(@RequestParam("categoryId") Long categoryId) {
        categoryService.deleteCategory(categoryId);
        return ReturnResult.success("Ok");
    }

 /*   @ApiOperation("查看该分类是否有子孙类")
    @RequestMapping(value = "/getChildCategory", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ReturnResult<Void> getChildCategory(@RequestParam("categoryId") Long categoryId) {
        List<ServiceCategory> categoryList =  categoryService.getChildCategory(categoryId);
        if(categoryList.size() > 0) {
            return ReturnResult.success("该分类下还有分类");
        } else {
            List<ServiceProduct> productList = productService.findProductsByCategoryId(categoryId);
            if(productList.size() > 0) {
                return ReturnResult.success("该分类下还有产品，您确定是否删除");
            } else {
                return ReturnResult.success("是否确定删除");
            }
        }
    }*/

    @ApiOperation("分类详情")
    @RequestMapping(value = "/getCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ServiceCategory> getCategory(@RequestParam("categoryId") Long categoryId) {
        ServiceCategory category = categoryService.findById(categoryId);
        return ReturnResult.success(category);
    }

    @ApiOperation("分类列表")
    @RequestMapping(value = "/showCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceCategory>> showCategory(ServiceCategoryVo categoryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        Page<ServiceCategory> page = categoryService.findCategoryForPage(categoryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("获取所有分类信息")
    @RequestMapping(value = "/getAllCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ServiceCategory>> getAllCategory(@RequestParam(value = "type") String type) {
        Integer intType = Integer.parseInt(type);
        Example<ServiceCategory> example = ServiceCategory.createExample();
        example.createCriteria().andEqualTo("type",intType);
        List<ServiceCategory> list = categoryService.findListByExample(example);
        return ReturnResult.success(list);
    }

    @ApiOperation("分类信息")
    @RequestMapping(value = "/getCategoryMessage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ServiceCategory>> getCategoryMessage() {
        Example<ServiceCategory> example = ServiceCategory.createExample();
        List<ServiceCategory> list = categoryService.findListByExample(example);
        return ReturnResult.success(list);
    }

    @ApiOperation("商品列表")
    @RequestMapping(value = "/goodPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceProduct>> goodPage(@RequestParam(value = "type", defaultValue = "0") Integer type, @RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize") int pageSize, ProductQueryVo productQueryVo, HttpServletRequest request) {
        productQueryVo.setType(type);
        Page<ServiceProduct> serviceProductPage = productService.findProductPageByQueryVo(productQueryVo, pageNo, pageSize);
        return ReturnResult.success(serviceProductPage);
    }



    @ApiOperation("下架商品")
    @RequestMapping(value = "/offGood", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> offGood(@RequestParam(value = "productId") Long productId, @RequestParam(value = "status",defaultValue = "0")Integer status) {
            productService.offProduct(productId,status);
        return ReturnResult.success("下架成功");
    }

    @ApiOperation("删除商品")
    @RequestMapping(value = "/deleteGood", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteGood(@RequestParam(value = "productId") Long productId) {
        productService.deleteById(productId);
        return ReturnResult.success("下架成功");
    }

    @ApiOperation("需求列表")
    @RequestMapping(value = "/demandPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceProduct>> demandPage(@RequestParam(value = "type", defaultValue = "3") Integer type, @RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize") int pageSize, ProductQueryVo productQueryVo, HttpServletRequest request) {
        productQueryVo.setType(type);
        Page<ServiceProduct> serviceProductPage = productService.findProductPageByQueryVo(productQueryVo, pageNo, pageSize);
        return ReturnResult.success(serviceProductPage);
    }


    @ApiOperation("下架需求")
    @RequestMapping(value = "/offDemand", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> offDemand(@RequestParam(value = "productId") Long productId, @RequestParam(value = "status",defaultValue = "0")Integer status) {
        productService.offProduct(productId,status);
        return ReturnResult.success("下架成功");
    }

    @ApiOperation("删除需求")
    @RequestMapping(value = "/deleteDemand", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteDemand(@RequestParam(value = "productId") Long productId, @RequestParam(value = "status",defaultValue = "0")Integer status) {
        productService.deleteById(productId);
        return ReturnResult.success("删除成功");
    }


    @ApiOperation("用户可选的区域和业务组")
    @RequestMapping(value = "/ownTeam", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<TeamVo>> ownTeam(HttpServletRequest request) {
        SysUser user = getUser(request);
        if (user == null) {
            return ReturnResult.fail("未登录或者已过期");
        }
        List<TeamVo> teamVoList=serviceMerchantService.ownTeam(user);
        return ReturnResult.success(teamVoList);
    }


    @ApiOperation("租赁列表")
    @RequestMapping(value = "/leasePage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceLeaseProductDetail> > leasePage(@RequestParam(value = "type",defaultValue = "2") Integer type,@ModelAttribute ProductQueryVo productQueryVo , @RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize") int pageSize, LeaseMessageQueryVo leaseMessageQueryVo, HttpServletRequest request) {
        productQueryVo.setType(type);
        Page<ServiceLeaseProductDetail> serviceLeaseProductDetailPage=serviceProductService.findLeaseProductByQueryVo(productQueryVo,pageNo,pageSize);
        return ReturnResult.success(serviceLeaseProductDetailPage);
    }

    @ApiOperation("下架租赁")
    @RequestMapping(value = "/offLease", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> offLease(@RequestParam(value = "productId") Long productId, @RequestParam(value = "status",defaultValue = "0")Integer status, HttpServletRequest request) {
        SysUser sysUser=getUser(request);
        serviceProductService.onOrOff(sysUser,productId,status,0);
        return ReturnResult.success("下架成功");
    }

    @ApiOperation("删除租赁")
    @RequestMapping(value = "/deleteLease", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteLease(@RequestParam(value = "productId")Long productId, HttpServletRequest request) {
        SysUser sysUser=getUser(request);
        serviceProductService.deleteProductByUserIdAndProductId(sysUser.getUserId(),Arrays.asList(productId),0);
        return ReturnResult.success("下架成功");
    }



    @ApiOperation("校区列表")
    @RequestMapping(value = "/campusList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<Campus>> campusList(CampusQueryCondVo campusQueryCondVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        DataPermission dataPermission = getDataPermission(request);
        campusQueryCondVo.setAreaTeamIdList(dataPermission.getAreaTeamIds());
        Page<Campus> campusPage=campusService.findCampusByCondVo(campusQueryCondVo,pageNo,pageSize);
        return ReturnResult.success(campusPage);
    }

    @ApiOperation("校区详情")
    @RequestMapping(value = "/campusDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Campus> campusDetail(@RequestParam(value = "campusId") Long campusId, HttpServletRequest request ) {
        Campus campus=campusService.findById(campusId);
        return ReturnResult.success(campus);
    }


    @ApiOperation("审核校区")
    @RequestMapping(value = "/auditCampus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Campus> auditCampus(@RequestParam(value = "campusId") Long campusId, @RequestParam(value = "state") Integer state,@RequestParam(value = "remarks",required = false) String remarks, HttpServletRequest request ) {


        campusService.changeState(campusId,state,remarks);
        return ReturnResult.success("ok");
    }


    @ApiOperation("新建或者更新校区")
    @RequestMapping(value = "/addOrUpdateCampus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveUnAuditedCampus(CampusSaveOrUpdateVo campusSaveOrUpdateVo,HttpServletRequest request) {
        SysUser user = getUser(request);
        //campusSaveOrUpdateVo.setState(1);
        campusSaveOrUpdateVo.setUserId(user.getUserId());
        campusSaveOrUpdateVo.setUserPhoneNo(user.getPhoneNo());
        campusSaveOrUpdateVo.setUsername(user.getUsername());
        campusService.saveOrUpdateCampus(campusSaveOrUpdateVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("用户可选的主体")
    @RequestMapping(value = "/ownMainCount", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<MainAccount>> ownMainCount(HttpServletRequest request) {
        SysUser user = getUser(request);
        List<MainAccount> mainAccountList = serviceMerchantService.ownMainAccount(user);
        return ReturnResult.success(mainAccountList);
    }

    @ApiOperation("教室列表")
    @RequestMapping(value = "/roomList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<Room>> roomList(RoomQueryVo roomQueryVo,@RequestParam(value = "pageNo")int  pageNo,@RequestParam(value = "pageSize") int  pageSize, @RequestParam(value = "campusId") Long campusId, HttpServletRequest request) {
       Page<Room> page = roomService.findRoomByQueryForPage(roomQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("删除教室")
    @RequestMapping(value = "/delRoom", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> delRoom(@RequestParam("roomId") Long roomId,HttpServletRequest request) {
        roomService.deleteRoom(roomId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("根据年份和校区获得寒暑假时间")
    @RequestMapping(value = "/findSummerWinterDate", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<SummerWinterDate> findSummerWinterDate(@RequestParam(value = "campusId")Long campusId,@RequestParam(value = "year") String year, HttpServletRequest request) {
        SummerWinterDate summerWinterDate=summerWinterDateService.findByCampusIdAndYear(campusId,year);
        return ReturnResult.success(summerWinterDate);
    }



    @ApiOperation("新建或者更新教室")
    @RequestMapping(value = "/saveOrUpdateRoom", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateRoom(RoomSaveOrUpdateVo roomSaveOrUpdateVo, HttpServletRequest request) {
        roomService.saveOrUpdateRoom(roomSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("教室详情")
    @RequestMapping(value = "/roomDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Room> saveUnAuditedRoom(@RequestParam(value = "roomId") Long roomId, HttpServletRequest request) {
        Room room = roomService.findById(roomId);
        return ReturnResult.success(room);
    }

    @ApiOperation("服务商家详情")
    @RequestMapping(value = "/merchantDetail")
    @ResponseBody
    public ReturnResult<ServiceMerchant> merchantDetail(@RequestParam(value = "merchantId") Long merchantId){
        ServiceMerchant serviceMerchant = serviceMerchantService.findById(merchantId);
        return ReturnResult.success(serviceMerchant);
    }

    @ApiOperation("订单列表")
    @RequestMapping(value = "/orderItemList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceOrderItem>> orderItemList(ServiceOrderItemQueryVo serviceOrderItemQueryVo,@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize,HttpServletRequest request) {
        Page<ServiceOrderItem> serviceOrderItemPage = serviceOrderItemService.findByQueryCond(serviceOrderItemQueryVo, pageNo, pageSize);


        return ReturnResult.success(serviceOrderItemPage);
    }


}
